package com.example.tingsugar.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.tingsugar.bean.*;
import com.example.tingsugar.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author lzc
 * @Date 2023/4/16 09:02
 */
@RestController
public class SugarController {
    
    @Autowired
    TrafficService trafficService;
    
    @RequestMapping("/sugar/traffic")
    public String traffic(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        List<TrafficBean> list = trafficService.traffic(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
        
        JSONArray categories = new JSONArray();
        for (TrafficBean trafficBean : list) {
            categories.add(trafficBean.getCh());
        }
        data.put("categories", categories);
        
        JSONArray series = new JSONArray();
        
        JSONObject uvObj = new JSONObject();
        uvObj.put("name", "独立访客数");
        JSONArray uvData = new JSONArray();
        uvObj.put("data", uvData);
        series.add(uvObj);
        
        JSONObject svObj = new JSONObject();
        svObj.put("name", "会话数");
        JSONArray svData = new JSONArray();
        svObj.put("data", svData);
        series.add(svObj);
        
        JSONObject pvPerSvObj = new JSONObject();
        pvPerSvObj.put("name", "会话数平均页面数");
        JSONArray pvPerSvData = new JSONArray();
        pvPerSvObj.put("data", pvPerSvData);
        series.add(pvPerSvObj);
        
        
        JSONObject durationPerSvObj = new JSONObject();
        durationPerSvObj.put("name", "会话数平均停留时长");
        JSONArray durationPerSvData = new JSONArray();
        durationPerSvObj.put("data", durationPerSvData);
        series.add(durationPerSvObj);
        
        
        for (TrafficBean trafficBean : list) {
            uvData.add(trafficBean.getUv_ct());
            svData.add(trafficBean.getSv_ct());
            pvPerSvData.add(trafficBean.getPv_per_sv());
            durationPerSvData.add(trafficBean.getDuration_per_sv());
        }
        
        data.put("series", series);
        
        result.put("data", data);
        return result.toJSONString();
    }
    
    @Autowired
    UserService userService;
    
    @RequestMapping("/sugar/user")
    public String user(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        List<UserBean> list = userService.user(date);
        
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        JSONObject data = new JSONObject();
        data.put("total", list.size());
        
        JSONArray columns = new JSONArray();
        JSONObject c1 = new JSONObject();
        c1.put("name", "日期");
        c1.put("id", "date");
        columns.add(c1);
        JSONObject c2 = new JSONObject();
        c2.put("name", "新增用户数");
        c2.put("id", "new_user_ct");
        columns.add(c2);
        JSONObject c3 = new JSONObject();
        c3.put("name", "活跃用户数");
        c3.put("id", "active_user_ct");
        columns.add(c3);
        JSONObject c4 = new JSONObject();
        c4.put("name", "新增 vip 用户数");
        c4.put("id", "new_vip_ct");
        columns.add(c4);
        data.put("columns", columns);
    
        JSONArray rows = new JSONArray();
        for (UserBean userBean : list) {
            JSONObject row = new JSONObject();
            row.put("date", userBean.getDate() + "");
            row.put("new_user_ct", userBean.getNew_user_ct());
            row.put("active_user_ct", userBean.getActivate_user_ct());
            row.put("new_vip_ct", userBean.getNew_vip_ct());
            rows.add(row);
        }
        data.put("rows", rows);
        
        result.put("data", data);
        return result.toJSONString();
    }
    
    
    @Autowired
    AlbumServie albumServie;
    
    @RequestMapping("/sugar/album")
    public String album(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
    
        List<AlbumBean> list = albumServie.album(date);
    
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONObject data = new JSONObject();
    
        JSONArray columns = new JSONArray();
        JSONObject c1 = new JSONObject();
        c1.put("name", "日期");
        c1.put("id", "date");
        columns.add(c1);
        JSONObject c2 = new JSONObject();
        c2.put("name", "专辑名");
        c2.put("id", "album_name");
        columns.add(c2);
        JSONObject c3 = new JSONObject();
        c3.put("name", "播放人数");
        c3.put("id", "play_user_ct");
        columns.add(c3);
        JSONObject c8 = new JSONObject();
        c8.put("name", "播放时长");
        c8.put("id", "play_duration");
        columns.add(c8);
        JSONObject c4 = new JSONObject();
        c4.put("name", "播放次数");
        c4.put("id", "play_ct");
        columns.add(c4);
        JSONObject c5 = new JSONObject();
        c5.put("name", "下单人数");
        c5.put("id", "order_user_ct");
        columns.add(c5);
        JSONObject c6 = new JSONObject();
        c6.put("name", "下单次数");
        c6.put("id", "order_ct");
        columns.add(c6);
        JSONObject c7 = new JSONObject();
        c7.put("name", "下单金额");
        c7.put("id", "order_amount");
        columns.add(c7);
        JSONObject c9 = new JSONObject();
        c9.put("name", "订阅人数");
        c9.put("id", "subscribe_user_ct");
        columns.add(c9);
        JSONObject c10 = new JSONObject();
        c10.put("name", "订阅次数");
        c10.put("id", "subscribe_ct");
        columns.add(c10);
        JSONObject c11 = new JSONObject();
        c11.put("name", "评论人数");
        c11.put("id", "comment_user_ct");
        columns.add(c11);
        data.put("columns", columns);
    
        JSONArray rows = new JSONArray();
        for (AlbumBean albumBean : list) {
            System.out.println(albumBean);
            JSONObject row = new JSONObject();
            row.put("date", albumBean.getDate().toString());
            row.put("album_name", albumBean.getAlbum_name());
            row.put("play_user_ct", albumBean.getPlay_user_ct());
            row.put("play_ct", albumBean.getPlay_ct());
            row.put("play_duration", albumBean.getPlay_duration());
            row.put("order_user_ct", albumBean.getOrder_user_ct());
            row.put("order_ct", albumBean.getOrder_ct());
            row.put("order_amount", albumBean.getOrder_amount());
            row.put("subscribe_user_ct", albumBean.getSubscribe_user_ct());
            row.put("subscribe_ct", albumBean.getSubscribe_ct());
            row.put("comment_user_ct", albumBean.getComment_user_ct());
            rows.add(row);
        }
        data.put("rows", rows);
    
    
        result.put("data", data);
        return result.toJSONString();
    }
    
    
    @Autowired
    UserPlayService userPlayService;
    
    @RequestMapping("/sugar/user/play/avg_duration")
    public String userAlbumAvgDuration(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
    
    
        List<AlbumAvgPlayDurationBean> list = userPlayService.statsPlayDuration(date);
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
    
        JSONArray data = new JSONArray();
    
        for (AlbumAvgPlayDurationBean bean : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", bean.getAlbum());
            obj.put("value", bean.getAvg_play_duration());
            data.add(obj);
        }
        result.put("data", data);
        return result.toJSONString();
    }
    
    @RequestMapping("/sugar/user/play/duration")
    public String userAvgDuration(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
        
        
        List<AlbumAvgPlayDurationBean> list = userPlayService.statsPlayDuration(date);
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
    
        result.put("data", userPlayService.avgDuration(date));
        return result.toJSONString();
    }
    
    
    @RequestMapping("/sugar/user/play/hour/duration")
    public String hourDuration(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
    
        List<HourPlayDurationBean> list = userPlayService.statsHourPlay(date);
        Map<Integer, HourPlayDurationBean> hourToBeanMap = new HashMap<>();
        for (HourPlayDurationBean bean : list) {
            hourToBeanMap.put(bean.getHour(), bean);
        }
    
    
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        JSONObject data = new JSONObject();
    
        JSONArray categories = new JSONArray();
        for (int hour = 0; hour < 24; hour++) {
            categories.add(hour);
        }
        data.put("categories", categories);
    
        JSONArray series = new JSONArray();
    
        JSONObject obj1 = new JSONObject();
        obj1.put("name", "平均播放时长(分钟)");
    
        JSONArray obj1Data = new JSONArray();
        for (int hour = 0; hour <24; hour++) {
            HourPlayDurationBean bean = hourToBeanMap.getOrDefault(hour, new HourPlayDurationBean(hour, new BigDecimal(0)));
            obj1Data.add(bean.getAvg_play_duration());
        }
        obj1.put("data", obj1Data);
        series.add(obj1);
    
        data.put("series", series);
    
        result.put("data", data);
        return result.toJSONString();
    }
    
    @Autowired
    PayService payService;
    @RequestMapping("/sugar/pay/pay_item_amount")
    public String payItemAmount(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
    
    
        List<PayItemRateBean> list = payService.statsPayItem(date);
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        
        for (PayItemRateBean bean : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", bean.getPay_item_name());
            obj.put("value", bean.getPay_amount());
            data.add(obj);
        }
        result.put("data", data);
        return result.toJSONString();
    }
    @RequestMapping("/sugar/pay/vip")
    public String payVip(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
    
    
        List<PayVipDurationRateBean> list = payService.statsVipDurationRate(date);
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
        
        JSONArray data = new JSONArray();
        
        for (PayVipDurationRateBean bean : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", bean.getVip_duration());
            obj.put("value", bean.getUser_ct());
            data.add(obj);
        }
        result.put("data", data);
        return result.toJSONString();
    }
    
    
    @RequestMapping("/sugar/pay/recharge")
    public String payRecharge(Integer date) {
        if (date == null) { // 如果没有传入日期,则使用今天做日期
            date = Integer.parseInt(new SimpleDateFormat("yyyyMMdd").format(new Date()));
        }
    
    
        List<PayRechargeRangeBean> list = payService.statsRechargeRangeRate(date);
        JSONObject result = new JSONObject();
        
        result.put("status", 0);
        result.put("msg", "");
    
        JSONArray data = new JSONArray();
    
        for (PayRechargeRangeBean bean : list) {
            JSONObject obj = new JSONObject();
            obj.put("name", bean.getRecharge_amount_range());
            obj.put("value", bean.getRecharge_ct());
            data.add(obj);
        }
        result.put("data", data);
        return result.toJSONString();
    }
}


